package com.myfum.wingplayer.components.decorators
{
	import com.myfum.wingplayer.components.containers.AbstractContainer;
	
	import flash.events.Event;
	import flash.events.NetStatusEvent;
	import flash.media.SoundTransform;
	import flash.media.Video;
	import flash.net.NetConnection;
	import flash.net.NetStream;
	
	public class VideoDecorator extends AbstractDecorator
	{
		private var _conn:NetConnection;
		private var _stream:NetStream;
		private var _video:Video;
		
		public function VideoDecorator(container:AbstractContainer)
		{
			super(container);
			init();
		}
		
		private function init():void{
			_conn = new NetConnection();
			_conn.connect(null);
			_stream = new NetStream(_conn);
			_stream.client = new Client();
			_video = new Video();
			_video.attachNetStream(_stream);
		}
		
		override public function getReady():void{
			container.data = data;
			
			playStream(container.data.path);
			_stream.addEventListener(NetStatusEvent.NET_STATUS, onStatus);
			var sf:SoundTransform = _stream.soundTransform;
			sf.volume = mute ? 0:1;
			_stream.soundTransform = sf;
			container.holder.addChild(_video);
			container.getReady();
		}
		
		override public function dispose():void{
			try{
				_stream.removeEventListener(NetStatusEvent.NET_STATUS, onStatus);
				_video.clear();
				_stream.pause();
				_stream.close();
				_conn.close();
				_video = null;
				_stream = null;
				_conn = null;
			}catch(e:Error){
				
			}
			super.dispose();
		}
		
		private function playStream(path:String):void{
			var extend:String = path.split(".").pop();
			switch(extend.toLowerCase()){
				case "flv":
					_stream.play(path);
					break;
				case "f4v":
				case "mp4":
				case "m4v":
				case "3gpp":
					_stream.play(path);
					break;
			}
		}
		
		private function onStatus(evt:NetStatusEvent):void{
			trace(evt.info.code);
			switch(evt.info.code){
				case "NetStream.Play.Start":
					dispatchEvent(new Event(Event.COMPLETE));
					break;
				case "NetStream.Play.Stop":
					_stream.play(container.data.path);
					break;
			}
		}
		
		override public function play():void{
			container.play();
		}
		
		override public function set width(value:Number):void{
			super.width = value;
			_video.width = width;
		}
		
		override public function set height(value:Number):void{
			super.height = value;
			_video.height = height;
		}
	}
}
class Client
{
	public function onMetaData(obj:Object):void{
		
	}
	
	public function onXMPData(obj:Object):void{
		
	}
}